package com.crhms.seabow.controller;

import com.crhms.seabow.interceptor.SystemControllerLog;
import com.crhms.seabow.model.LoginDto;
import com.crhms.seabow.entity.Role;
import com.crhms.seabow.entity.User;
import com.crhms.seabow.service.ILoginService;
import com.crhms.seabow.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @Autowired
    private UserService userService;

    @SystemControllerLog(description = "登录")
    @PostMapping(value = "/login")
    public User login(@RequestBody LoginDto loginInfo, HttpServletRequest request, HttpServletResponse response) {



        return null;
    }
    @GetMapping(value = "/logout")
    @SystemControllerLog(description = "退出")
    public ResponseEntity<Void> logout() {

        return ResponseEntity.ok().build();
    }
  /*  @GetMapping(value = "/logout")
    public ResponseEntity<Void> logout() {
        Subject subject = SecurityUtils.getSubject();
        if(subject.getPrincipals() != null) {
            User user = (User)subject.getPrincipals().getPrimaryPrincipal();
            userService.deleteLoginInfo(user.getName());
        }
        SecurityUtils.getSubject().logout();
        return ResponseEntity.ok().build();
    }*/

    /*@RequestMapping(value = "/login", method = RequestMethod.GET)
    public String login() {
        return "login";
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@RequestBody Map map) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(
                map.get("username").toString(),
                map.get("password").toString());

        subject.login(usernamePasswordToken);
        return "login";
    }*/

    @RequestMapping(value ="/index",method = RequestMethod.POST)
    public String index() {
        return "index";
    }

/*    @RequestMapping(value ="/logout",method = RequestMethod.POST)
    public String logout() {
        return "logout";
    }*/

    //错误页面展示
    //@RequestMapping(value = "/error",method = RequestMethod.POST)
    public String error(){
        return "error ok!";
    }

    //数据初始化
    @RequestMapping(value = "/addUser",method = RequestMethod.POST)
    public String addUser(){
        Map<String,Object> map =new HashMap<>();
        map.put("username", "admin");
        map.put("password", "123456");
        User user = loginService.addUser(map);
        return "addUser is ok! \n" + user;
    }

    //角色初始化
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    public String addRole(@RequestBody Map<String,Object> map){
        Role role = loginService.addRole(map);
        return "addRole is ok! \n" + role;
    }

    //注解的使用
/*    @RequiresRoles("admin")
    @RequiresPermissions("create")
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String create(){
        return "Create success!";
    }*/
}
